//
//  downController.m
//  XMLYFM
//
//  Created by dllo on 15/12/22.
//  Copyright © 2015年 zzl. All rights reserved.
//

#import "downController.h"
#import "ZTDownloadCollectionViewCell.h"
#import "AppTools.h"
#import "DownModel.h"
#import "ZTDownDetailViewController.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface downController () <UICollectionViewDataSource, UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *downloadCollectionView;

@property (nonatomic, strong) NSMutableArray *downloadArray;

@end

@implementation downController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"分类听";
    
    [self setAutomaticallyAdjustsScrollViewInsets:YES];
    
#pragma mark - 创建flowLayout
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.itemSize = CGSizeMake(WIDTH / 4, 110);
    
    
#pragma mark - 创建UICollectionView
    self.downloadCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 44) collectionViewLayout:flowLayout];
    [self.view addSubview:self.downloadCollectionView];
    self.downloadCollectionView.backgroundColor = [UIColor whiteColor];
    self.downloadCollectionView.showsVerticalScrollIndicator = NO;
    self.downloadCollectionView.delegate = self;
    self.downloadCollectionView.dataSource = self;
    
    [self.downloadCollectionView registerClass:[ZTDownloadCollectionViewCell class] forCellWithReuseIdentifier:@"downloadCell"];
    
    //初始化数组
    self.downloadArray = [NSMutableArray array];
    
    [self getData];
    
}


#pragma mark - 获取数据
-(void)getData
{
    NSString *fileName = @"Category.plist";
    [AppTools getDataWithFileName:fileName Block:^(id result) {
        
        for (NSMutableDictionary *dic in result) {
            
            DownModel *downModel = [DownModel modelWithDictionary:dic];
            [self.downloadArray addObject:downModel];
        }
        [self.downloadCollectionView reloadData];
    } ErrorBlock:^(NSError *error) {
        
        
    }];

    
}

#pragma mark - UICollectionView的协议方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return 20;
}

// 创建item
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZTDownloadCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"downloadCell" forIndexPath:indexPath];
    
    DownModel *model = [self.downloadArray objectAtIndex:indexPath.item];
    
    NSString *imageName = [NSString stringWithFormat:@"%@@2x.png", model.itemIcon];
    cell.iconImageView.image = [UIImage imageNamed:imageName];
    cell.titleLabel.text = model.itemTitle;
    
    return cell;
}



// 点击item触发
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZTDownDetailViewController *downDetailVC = [[ZTDownDetailViewController alloc] init];
//    [(TOTabBarController*)self.tabBarController isHiddenTabBar:YES withViewController:categorySVC];
    downDetailVC.categoryId = ((DownModel *)self.downloadArray[indexPath.item]).itemKey;
    [self.navigationController pushViewController:downDetailVC animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
